package com.baizhiedu.rsm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baizhiedu.rsm.dao.ScheduleDao;
import com.baizhiedu.rsm.entity.Student;
import com.baizhiedu.rsm.entity.StudentMessage;
import com.baizhiedu.rsm.service.ImportExportService;
import com.baizhiedu.rsm.service.StudentMessageServcie;
import com.baizhiedu.rsm.service.StudentServcie;
import com.baizhiedu.rsm.util.ExportExcelForStudent;
import com.baizhiedu.rsm.util.ParseExcelForStudent;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.tools.ant.taskdefs.condition.Http;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @author lby
 */
@Controller
@RequestMapping("student")
public class StudentController {
    @Autowired
    private ImportExportService importExportService;
    @Autowired
    private StudentServcie studentServcie;
    @Autowired
    private StudentMessageServcie studentMessageServcie;

    /**
     * 添加学生信息 默认添加到当前班级
     * 从Session中获取对应的班级id
     * @param student
     * @return
     */
    @RequestMapping("addStudent")
    public String addStudent(StudentMessage student, HttpSession session){
//        从Session中获取班级id
        student.setClassid((Integer) session.getAttribute("classid"));
        studentMessageServcie.insert(student);
        return "redirect:/showallstudent.html";
    }

    /**
     *  修改信息 行内编辑
     * @param student
     * @return
     */
    @RequestMapping("updateOne")
    @ResponseBody
    public Map updateOne(StudentMessage student){
        return studentMessageServcie.updateOne(student);
    }

    /**
     * 根据班级id获取对应班级的学生
     * id 从Session中获取
     * @return
     */
    @RequestMapping("showAllByClassId")
    @ResponseBody
    public List<StudentMessage> showAllByClassId(HttpSession session){
//        从Session中获取班级id
        Integer classid = (Integer) session.getAttribute("classid");
        return studentMessageServcie.showAllStudentByClassId(classid);
    }

    /**
     * 删除学生信息 所有被删除的学生加入deleteStudent表
     * @param datalist
     * @return
     */
    @RequestMapping("deleteSelete")
    @ResponseBody
    public String deleteSelete(String datalist){
        List<StudentMessage> students = JSON.parseObject(datalist,new TypeReference<List<StudentMessage>>(){});
        studentMessageServcie.deleteSelect(students);
        return "";
    }

    /**
     * 导出学生信息
     * //之后异步请求传递数组：加上此属性： traditional:true  防止参数深度序列化的  ids=1  ids=2
     * @param ids
     * @param req
     * @param res
     * @return
     */
    @RequestMapping("/exportMessage")
    public @ResponseBody Map<String,Object> exportMessage(Integer[] ids, HttpServletRequest req, HttpServletResponse res){
        return importExportService.exportStudent(ids,req,res);
    }
    /**
     * 导入用户。
     *
     * @param request
     *            HttpServletRequest 用来存储和获取数据。
     * @param response
     *            HttpServletResponse 用来响应数据信息。
     * @return 重定向到展示用户列表。
     */
    @RequestMapping("/importStudent")
    public String importStudent(HttpSession session,HttpServletRequest request, MultipartFile fileName, HttpServletResponse response) throws IOException {
        try {
            //        从Session中获取班级id
            Integer sessionClassId = (Integer) session.getAttribute("classid");
            InputStream inputStream = fileName.getInputStream();
            importExportService.importStudent(inputStream,sessionClassId);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/showallstudent.html";
    }
}
